package org.rcy.ruicingmarket.controller;


import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.ruicingmarket.address.AddressVo;
import org.rcy.ruicingmarket.address.AddressService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author RuicyWu
 * @Date 2022/3/11 8:56
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AddressService addressService;

    @PostMapping("/create")
    public ResultMessage addAddress(HttpServletRequest request, @RequestBody AddressVo vo){
        try {
            String uid = (String)request.getHeader("uid");
            if(StringUtils.isEmpty(uid)){
                return ResultMessage.error();
            }
            vo.setUid(Long.parseLong(uid));
            int count = addressService.countAddress(vo.getUid());
            if(count==3){
                return ResultMessage.error();
            }
            addressService.create(vo);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @PutMapping("/edit")
    public ResultMessage editAddress(@RequestBody AddressVo vo){
        try {
            addressService.editAddress(vo);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @DeleteMapping("/del/{id}")
    public ResultMessage delAddress(@PathVariable("id")Long id){
        try {
            addressService.delAddress(id);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @GetMapping("/list")
    public ResultMessage getAddressList(HttpServletRequest request){
        try {
            String uid = (String) request.getHeader("uid");
            if(StringUtils.isEmpty(uid)){
                return ResultMessage.error();
            }
            return ResultMessage.success(addressService.queryAddressListByUid(Long.parseLong(uid)));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @GetMapping("/{id}")
    public ResultMessage getById(@PathVariable("id")Long id){
        try {
            return ResultMessage.success(addressService.getById(id));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }
}
